package com.byq.ai.agentlab.controller;

import com.byq.ai.agentlab.entity.Task;
import com.byq.ai.agentlab.entity.TaskStatus;
import com.byq.ai.agentlab.service.PatternSelecttor;
import com.byq.ai.agentlab.service.TaskService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/task")
@CrossOrigin(origins = "*")
public class TaskController {
    @Autowired
    private PatternSelecttor selecttor;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private TaskService taskService;

    @PostMapping("/submit")
    public Task submitTask(@RequestBody TaskRequest request) {
        PatternSelecttor.ColaborationPlan selection = selecttor.selectPattern(request.getDescription());
        
        // Create and save the task
        Task task = taskService.createTask(
            request.getName(),
            request.getDescription(),
                "{}"
        );
        
        // Send to message queue
        rabbitTemplate.convertAndSend("taskQueue", task.getDescription());
        
        return task;
    }

    @GetMapping("/{taskId}")
    public Task getTaskStatus(@PathVariable Long taskId) {
        return taskService.getTask(taskId);
    }

    @GetMapping("/similar")
    public List<Task> findSimilarTasks(
            @RequestParam String query,
            @RequestParam(defaultValue = "5") int limit) {
        return taskService.findSimilarTasks(query, limit);
    }

    @PutMapping("/{taskId}/status")
    public Task updateTaskStatus(
            @PathVariable Long taskId,
            @RequestParam TaskStatus status) {
        return taskService.updateTaskStatus(taskId, status);
    }
}
